﻿using System.Xml;

namespace Kioskvalt.MidiSequencer
{
    public class Instrument
    {
        public string ID;
        public string Name;
        public string Type;
        public int MidiDevice;
        public int MidiChannel;
        public int MidiPatch;
        public string DefaultEditorUI;
        public string DefaultDeviceUI;

        public Instrument()
        {
            ID = "";
            Name = "";
            Type = "";
            MidiDevice = 0;
            MidiChannel = 0;
            MidiPatch = 0;
            DefaultDeviceUI = "";
            DefaultEditorUI = "";
        }

        public bool LoadFromXML(XmlNode node)
        {
            if (node.Attributes["id"] != null)
            {
                ID = node.Attributes["id"].Value;
            }

            if (node.Attributes["name"] != null)
            {
                Name = node.Attributes["name"].Value;
            }

            if (node.Attributes["type"] != null)
            {
                Type = node.Attributes["type"].Value;
            }

            if (node.Attributes["editorui"] != null)
            {
                DefaultEditorUI = node.Attributes["editorui"].Value;
            }

            if (node.Attributes["deviceui"] != null)
            {
                DefaultDeviceUI = node.Attributes["deviceui"].Value;
            }

            if (node.Attributes["midichannel"] != null)
            {
                int i;
                int.TryParse(node.Attributes["midichannel"].Value, out i);
                MidiChannel = i;
            }

            if (node.Attributes["midipatch"] != null)
            {
                int i;
                int.TryParse(node.Attributes["midipatch"].Value, out i);
                MidiPatch = i;
            }
            if (node.Attributes["mididevice"] != null)
            {
                int i;
                int.TryParse(node.Attributes["mididevice"].Value, out i);
                MidiDevice = i;
            }

            return true;
        }

        public bool SaveToXML(XmlNode parent)
        {
            XmlNode rootel = Utilities.AddXmlElement(parent, "instrument");
            Utilities.AddXmlAttribute(rootel, "id", ID);
            Utilities.AddXmlAttribute(rootel, "name", Name);
            Utilities.AddXmlAttribute(rootel, "type", Type);
            Utilities.AddXmlAttribute(rootel, "midichannel", MidiChannel.ToString());
            Utilities.AddXmlAttribute(rootel, "midipatch", MidiPatch.ToString());
            Utilities.AddXmlAttribute(rootel, "mididevice", MidiDevice.ToString());
            Utilities.AddXmlAttribute(rootel, "deviceui", DefaultDeviceUI);
            Utilities.AddXmlAttribute(rootel, "editorui", DefaultEditorUI);

            return false;
        }

        public Instrument Clone()
        {
            var ret = new Instrument
            {
                ID = ID,
                Name = Name,
                Type = Type,
                MidiPatch = MidiPatch,
                MidiChannel = MidiChannel,
                MidiDevice = MidiDevice
            };
            return ret;
        }
    }
}
